﻿using System;
using System.IO;

namespace Lucene.Net.Store.S3 {

	public class StreamOutput : Stream {
		private Lucene.Net.Store.IndexOutput output;

		public StreamOutput(Lucene.Net.Store.IndexOutput output) {
			this.output = output;
		}

		public override Boolean CanRead { get { return false; } }
		public override Boolean CanSeek { get { return true; } }
		public override Boolean CanWrite { get { return true; } }
		public override Int64 Length { get { return this.output.Length(); } }

		public override void Flush() {
			this.output.Flush();
		}

		public override Int64 Position {
			get {
				return this.output.GetFilePointer();
			}
			set {
				this.output.Seek(value);
			}
		}

		public override Int32 Read(Byte[] buffer, Int32 offset, Int32 count) {
			throw new NotImplementedException();
		}

		public override Int64 Seek(Int64 offset, SeekOrigin origin) {
			switch (origin) {
				case SeekOrigin.Begin:
					this.output.Seek(offset);
					break;
				case SeekOrigin.Current:
					this.output.Seek(this.output.GetFilePointer() + offset);
					break;
				case SeekOrigin.End:
					throw new NotImplementedException();
			}
			return this.output.GetFilePointer();
		}

		public override void SetLength(Int64 value) {
			throw new NotImplementedException();
		}

		public override void Write(Byte[] buffer, Int32 offset, Int32 count) {
			this.output.WriteBytes(buffer, offset, count);
		}

		public override void Close() {
			this.output.Flush();
			this.output.Close();
			base.Close();
		}
	}
}